package test0319;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/3/19 22:36
 * 有三个线程，线程名称分别为：a，b，c。
 * 每个线程打印自己的名称。
 * 需要让他们同时启动，并按 c，b，a的顺序打印
 */
public class Main02 {

    static Object loopA = new Object();
    static Object loopB = new Object();
    static Object loopC = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(() -> {
            try {
                synchronized (loopA) {
                    loopA.wait();
                }
                System.out.println("a: " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread b = new Thread(() -> {
            try {
                synchronized (loopB) {
                    loopB.wait();
                }
                System.out.println("b: " + Thread.currentThread().getName());
                synchronized (loopA) {
                    loopA.notify(); // 通知 A 打印
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread c = new Thread(() -> {
            try {
                synchronized (loopC) {
                    loopC.wait();
                }
                System.out.println("c: " + Thread.currentThread().getName());
                synchronized (loopB) {
                    loopB.notify(); // 通知 B 打印
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // 启动三个线程
        a.start();
        b.start();
        c.start();

        Thread.sleep(1000);

        // 从 C 开始
        synchronized (loopC) {
            loopC.notify();
        }
    }
}
